<?php

namespace App\Admin\Controllers\Shop;

use App\Admin\Actions\Grid\Shop\ShopExamineAction;
use App\Admin\Actions\Grid\Shop\ShopGuaranteeAction;
use App\Models\Shop\WxShop;
use App\Wen\Utils\Settings;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class WxShopController extends AdminController
{

    public function shops(Request $request)
    {
        $q = $request->get('q');
        $admin_user = Admin::user();
        if($admin_user->isAdministrator()){
            return WxShop::where('shop_name', 'like', "%$q%")->orWhere('id', (int)$q)->orWhere('user_id', (int)$q)->paginate(null, ['id', 'shop_name as text']);
        }else{
            if(_empty_($admin_user->uid)){
                return WxShop::where('shop_name', 'like', "%$q%")->orWhere('id', (int)$q)->orWhere('user_id', (int)$q)->paginate(null, ['id', 'shop_name as text']);
            }else{
                return WxShop::where('user_id', $admin_user->uid)->where('shop_name', 'like', "%$q%")->orWhere('id', (int)$q)->orWhere('user_id', (int)$q)->orWhere('user_id', (int)$q)->paginate(null, ['id', 'shop_name as text']);
            }
        }
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxShop(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id','user_id','user.user_name'])->placeholder('搜索店铺ID，用户ID，用户名字...')->width(50);
            $grid->column('id')->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('shop_classifys');
            $grid->column('margin_rest')->display(function ($v){
                return '<a href="'.admin_url('shop/guarantee?shop_id='.$this->id).'" target="_blank">￥'.$v.'</a>';
            });
            $grid->column('logo');
            $grid->column('name');
            $grid->column('introduce');
//            $grid->column('country');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('district');
            $grid->column('business_license_image');
//            $grid->column('phone');
//            $grid->column('wechat');
//            $grid->column('qq');
            $grid->column('head_name');
            $grid->column('head_photo');
            $grid->column('head_id_card_a');
            $grid->column('head_id_card_b');
            $grid->column('supplement');
            $grid->column('status')->using([0=>'审核中', 1=>'审核通过',2=>'审核拒绝', 3=> '系统惩罚下线', 4 => '管理员惩罚下线', 5=> '店主关停'])->display(function ($v){
                if($v == '审核中'){
                    if($this->margin_rest >= $this->margin_least){
                        return '待审核';
                    }else{
                        return '待缴纳保证金';
                    }
                }
                return $v;
            })->label([0=>'pink', 1 => 'primary', 2=> 'red', 3=>'grey', 4=>'grey', 5=>'grey']);
            $grid->column('refuse_reason');
            $grid->column('created_at')->sortable();
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->header(function ($collection) {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block;margin-top: 20px;">重要：请确保后台拥有“商家”角色，并且建议给商家配置基础权限，除了基础权限，别的也不用配，需要我先适配<a style="color: red;" target="_blank" href="https://img.mini.minisns.cn/images/page/enterprise.png">请看教程</a></div>';
            });


            // 禁止后台新建店铺
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchActions();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new ShopExamineAction())->setKey($this->id));
                $actions->append((new ShopGuaranteeAction())->setKey($this->id));
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShop(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->column('shop_classifys');
            $show->column('margin_rest');
            $show->field('logo');
            $show->field('name');
            $show->field('introduce');
            $show->field('country');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('business_license_image');
            $show->field('phone');
            $show->field('wechat');
            $show->field('qq');
            $show->field('head_name');
            $show->field('head_photo');
            $show->field('head_id_card_a');
            $show->field('head_id_card_b');
            $show->field('supplement');
            $show->field('status');
            $show->field('refuse_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxShop(), function (Form $form) {
            $form->display('id');

            $form->select('user_id')->options(function ($id) {
                $user = \App\Models\User\WxUser::find($id);
                if ($user) {
                    return [$user->id => $user->user_name];
                }
            })->ajax('select/users')->required();

            $form->text('logo');
            $form->text('name')->required();
            $form->textarea('introduce');
            $form->text('country');
            $form->text('province');
            $form->text('city');
            $form->text('district');
            $form->text('business_license_image');
            $form->text('phone');
            $form->text('wechat');
            $form->text('qq');
            $form->text('head_name');
            $form->text('head_photo');
            $form->text('head_id_card_a');
            $form->text('head_id_card_b');
            $form->textarea('supplement');
            $form->radio('status')->options([0=>'审核中', 1=>'审核通过',2=>'审核拒绝', 3=> '系统惩罚下线', 4 => '管理员惩罚下线'])->when(2, function (Form $form){
                $form->textarea('refuse_reason');
            });

            $form->display('created_at');
            $form->display('updated_at');


            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
